package cn.xttc.edu.controller;


import cn.xttc.edu.entity.Order;
import cn.xttc.edu.entity.ReturnMsg;
import cn.xttc.edu.entity.vo.OrderVo;
import cn.xttc.edu.service.OrderService;
import cn.xttc.edu.util.MyUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杨磊
 * @since 2022-03-09
 */
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 根据条件查询相关订单
     *
     * @param pn     页码
     * @param limit  每页显示行数
     * @param search 搜索参数
     * @param status 订单状态
     * @return json字符串
     */
    @GetMapping("/orders")
    public String getOrdersByCondition(@RequestParam("page") Integer pn,
                                       @RequestParam("limit") Integer limit,
                                       @RequestParam(value = "search", required = false, defaultValue = "") String search,
                                       @RequestParam("status") Integer status
    ) {
        Page<OrderVo> page = new Page<>(pn, limit);
        orderService.getOrderVosByCondition(page, status, search);
        return MyUtil.formatReturnData(page);
    }

    /**
     * 获取订单总数
     *
     * @return 包装对象
     */
    @GetMapping("/orders/totalNum")
    public ReturnMsg getTotalNum() {
        long count = orderService.count();
        return ReturnMsg.success(count);
    }

    /**
     * 获取今天的订单数
     *
     * @return 包装对象
     */
    @GetMapping("/orders/todayNum")
    public ReturnMsg getTodayNum() {
        LocalDateTime today = LocalDateTime.now();
        String todayStr = today.toString().substring(0, today.toString().indexOf("T")) + " 00:00:00";
        LocalDateTime tomorrow = today.plusDays(1);
        String tomorrowStr = tomorrow.toString().substring(0, tomorrow.toString().indexOf("T")) + " 00:00:00";
        long count = orderService.count(new QueryWrapper<Order>().between("create_time", todayStr, tomorrowStr));
        return ReturnMsg.success(count);
    }

    /**
     * 获取每周订单数
     * @return 包装对象
     */
    @GetMapping("/weeklyOrderNum")
    public ReturnMsg getWeeklyOrderNum() {
        //获取当前日期一周的日期
        List<String> weeklyDate = MyUtil.getWeeklyDate();
        ArrayList<Long> orderNumBerList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            long orderNum = orderService.count(new QueryWrapper<Order>().between("create_time", weeklyDate.get(i), weeklyDate.get(i + 1)));
            orderNumBerList.add(orderNum);
        }
        return ReturnMsg.success(orderNumBerList);
    }

    /**
     * 根据id查询订单
     *
     * @param id     订单号
     * @param status 状态
     * @return 包装对象
     */
    @GetMapping("/orders/{id}")
    public ReturnMsg getOrderById(@PathVariable("id") String id,
                                  @RequestParam("status") Integer status) {
        IPage<OrderVo> page = orderService.getOrderVosByCondition(new Page<>(1, 1), status, id);
        return ReturnMsg.success(page.getRecords().get(0));
    }

    /**
     * 单个删除和批量删除
     *
     * @param ids id字符串
     * @return 封装对象
     */
    @DeleteMapping("/orders/{ids}")
    public ReturnMsg deleteOrders(@PathVariable("ids") String ids) {
        if (ids.contains("-")) {
            //批量删除
            List<Integer> delIds = new ArrayList<>();
            String[] strIds = ids.split("-");
            //组装id的集合
            for (String string : strIds) {
                delIds.add(Integer.parseInt(string));
            }
            try {
                orderService.removeBatchByIds(delIds);
            } catch (Exception e) {
                return ReturnMsg.fail("删除失败");
            }
        } else {
            //单个删除
            Integer id = Integer.parseInt(ids);
            try {
                orderService.removeById(id);
            } catch (Exception e) {
                return ReturnMsg.fail("删除失败");
            }
        }
        return ReturnMsg.success("删除成功");
    }

}

